<?php

/**
*
* @package DND Charlist
* @version $Id: dnd_edit.php$
* @copyright (c) 2010 Stephan "MOTU" König
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/



// Benötigte Dateien und Variablen von phpBB
define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Session auslesen und Benutzer-Informationen laden
$user->session_begin();
$user->setup();
$auth->acl($user->data);

$user->setup('mods/info_dnd_list_chars');

// Initial var setup
$user_id	= request_var('u', ANONYMOUS);
$username	= request_var('un', '', true);


$intype	= (isset($_GET['intype'])) ? request_var('intype','') : 'alignment';		//gibt an, was neu erstellt werden soll
$mode	= (isset($_GET['mode'])) ? request_var('mode','') : 'show';
$inid	= (isset($_GET['inid'])) ? request_var('inid', '') : false;
$in_class_id = (isset($_GET['class_id'])) ? request_var('class_id', '') : 0;
//$mode_template = (strcmp($mode,'edit') == 0) ? true : false;

$submit = (isset($_POST['submit'])) ? true : false;

$in_name	= (isset($_POST['in_name'])) ? request_var('in_name','',true) : false;
$in_short	= (isset($_POST['in_short'])) ? request_var('in_short','',true) : false;
$in_info	= (isset($_POST['in_info'])) ? request_var('in_info','',true) : false;
$in_extra	= (isset($_POST['in_extra'])) ? request_var('in_extra','',true) : 0;
$in_spell	= (isset($_POST['in_spell'])) ? $_POST['in_spell'] : 0;
$in_skill	= (isset($_POST['in_skill'])) ? $_POST['in_skill'] : 0;
$in_class	= (isset($_POST['in_class'])) ? $_POST['in_class'] : 0;
$in_clf		= (isset($_POST['in_clf'])) ? $_POST['in_clf'] : 0;




//$s_hidden_fields = false;


$char_data = array();
$chars = array();
$configure = $dndEdit->getConfigure();
$access_rights = $configure['insert_page_accessible_by'];
$i = 0;

/*
* Inhaltsbereich
*/

// Header und Titel der Seite
page_header('DnD Char List');

// Angabe eurer Content-Seite
$template->set_filenames(array(
	'body' => 'dnd_edit.html')
);

if ($user->data['user_id'] == ANONYMOUS)
{
	// Code, wenn Benutzer nicht angemeldet ist
	login_box();
}
else
{
	// Code, wenn Benutzer angemeldet ist
	
	if($access_rights != 2)
	{
		if($access_rights == 1 && !($auth->acl_get('a_') || $auth->acl_getf_global('m_')))
		{
			//Nutzer muss Admin oder  Mod sein, ist es aber nicht
			login_box();
		}
		else if($access_rights == 0 && !($auth->acl_get('a_')))
		{
			//Nutzer muss Admin sein, ist es aber nicht
			login_box();
		}
		else
		{
			trigger_error('Error in access_rights for this page. Contact your administrator!');
			login_box();
		}
	}

	$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", "mode=$mode&amp;intype=$intype");

	if($intype != 'classe' && $intype != 'clf' && $intype != 'spell' && $intype != 'skill' && isIntype($intype))
	{
		if($mode == 'show')
		{
			$dndEdit->getSmall($intype);
			//var_dump($dndEdit->tmp->tables);
			
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$template->assign_block_vars('list_' . $intype, array(
					'NAME'		=> $row['name'],
					'SHORT'		=> $row['short'],
					'INFO'		=> $row['info'],
					'EXTRA'		=> $row['extra'],
					'U_EDIT'	=> $row['edit_link'],
					'U_DELETE'	=> $row['delete_link'],
				));
			}
			$dndEdit->freeTmp();
		}
		else if ($mode == 'edit' && $inid != false)
		{
			
			$dndEdit->getSmallById($inid, $intype);
			$row = $dndEdit->getTmp()->getSmall();
			$template->assign_vars(array(
				'A_NAME'	=> $row['name'],
				'A_SHORT'	=> $row['short'],
				'A_INFO'	=> $row['info'],
				'A_EXTRA'	=> $row['extra'],
			));
			$dndEdit->freeTmp();
			$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'edit', 'intype' => $intype, 'inid' => $inid), true);
		}
		else if ($mode == 'insert')
		{
			$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'submit', 'intype' => $intype), true);
		}
		else if ($mode == 'delete')
		{
			$dndEdit->getSmallById($inid, $intype);
			
			$dndEdit->getTmp()->deleteSmall();
			$dndEdit->freeTmp();
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
	}
	else if($intype == 'skill')
	{
		if($mode == 'show')
		{
			$dndEdit->getSkills(0);
			//var_dump("skills: ",$dndEdit->tmp->skills);
			foreach($dndEdit->getTmp()->getSkills() as $row)
			{
				$template->assign_block_vars('list_skill', array(
					'NAME'					=> $row['name'],
					'ATTRIBUTE'				=> $row['attribute_name'],
					'USE_UNTRAINED'			=> $row['use_untrained'],
					'ARMOR_CHECK_PENALTY'	=> $row['armor_check_penalty'],
					'INFO'					=> $row['info'],
					'U_EDIT'				=> $row['edit_link'],
					'U_DELETE'				=> $row['delete_link'],
				));
			}
			$dndEdit->freeTmp();
		}
		else if ($mode == 'edit' && $inid != false)
		{
			$s_hidden_fields = array('inid' => $inid);
			
			$dndEdit->getSkills($inid);
			$tmp = $dndEdit->getTmp()->getSkills();
			$template->assign_vars(array(
				'A_NAME'				=> $tmp['name'],
				'A_INFO'				=> $tmp['info'],
				'A_ATTRIBUTE'			=> $tmp['attribute_id'],
				'A_USE_UNTRAINED'		=> $tmp['use_untrained'],
				'A_ARMOR_CHECK_PENALTY'	=> $tmp['armor_check_penalty'],
			));
			$dndEdit->freeTmp();
			
			$dndEdit->getSmall('attribute');
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$template->assign_block_vars('list_skill_attributes', array(
					'A_ATTRIBUTE_NAME'		=> $row['name'],
					'A_ATTRIBUTE_ID'		=> $row['id'],
				));
			}
			$dndEdit->freeTmp();
			$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'edit', 'intype' => $intype, 'inid' => $inid), true);
		}
		else if ($mode == 'insert')
		{
			
			//$tmp = $dndEdit->tmp->getSkills();
			$dndEdit->getSmall('attribute');
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$template->assign_block_vars('list_skill_attributes', array(
					'A_ATTRIBUTE_NAME'		=> $row['name'],
					'A_ATTRIBUTE_ID'		=> $row['id'],
				));
			}
			$dndEdit->getSkills(-1);
			$dndEdit->freeTmp();
			$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'submit', 'intype' => $intype), true);
		}
		else if ($mode == 'delete')
		{
			$dndEdit->getSkills($inid);
			$dndEdit->getTmp()->deleteSkill();
			$dndEdit->freeTmp();
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
	}
	else if($intype == 'spell')
	{
		if($mode == 'show')
		{
			$dndEdit->getSpell();
			//var_dump("spells: ",$dndEdit->tmp->spells);
			foreach($dndEdit->getTmp()->getSpells() as $row)
			{
				//print_r($row);
				
				$template->assign_block_vars('list_' . $intype, array(
					'NAME'					=> $row['name'],
					'SCHOOL'				=> $row['school'],
					'LEVEL'					=> $row['level'],
					'COMPONENTS'			=> $row['components'],
					'CASTING_TIME'			=> $row['casting_time'],
					'RANGE'					=> $row['range'],
					'EFFECT'				=> $row['effect'],
					'DURATION'				=> $row['duration'],
					'SAVING_THROW'			=> $row['saving_throw'],
					'SPELL_RESISTANCE'		=> $row['spell_resistance'],
					'INFO'					=> $row['info'],
					'U_EDIT'				=> $row['edit_link'],
					'U_DELETE'				=> $row['delete_link'],
				));
			}
			$dndEdit->freeTmp();
		}
		else if ($mode == 'edit' && $inid != false)
		{
			$s_hidden_fields = array('inid' => $inid);
			
			$dndEdit->getSpellById($inid);
			$row = $dndEdit->getTmp()->getSpells();
			$template->assign_vars(array(
				'A_NAME'				=> $row['name'],
				'A_SCHOOL'				=> $row['school'],
				'A_LEVEL'				=> $row['level'],
				'A_COMPONENTS'			=> $row['components'],
				'A_CASTING_TIME'		=> $row['casting_time'],
				'A_RANGE'				=> $row['range'],
				'A_EFFECT'				=> $row['effect'],
				'A_DURATION'			=> $row['duration'],
				'A_SAVING_THROW'		=> $row['saving_throw'],
				'A_SPELL_RESISTANCE'	=> $row['spell_resistance'],
				'A_INFO'				=> $row['info'],
			));
			$dndEdit->freeTmp();
			$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'edit', 'intype' => $intype, 'inid' => $inid), true);
		}
		else if ($mode == 'insert')
		{
			$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'submit', 'intype' => $intype), true);
		}
		else if ($mode == 'delete')
		{
			$dndEdit->getSpellById($inid, $intype);
			$dndEdit->getTmp()->deleteSpell();
			$dndEdit->freeTmp();
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
	}
	else if($intype == 'classe')
	{
		if($mode == 'show')
		{
// 			$classes = $dndEdit->getClasses(0);
			foreach($dndEdit->getClasses(0)->getClasses() as $row)
			{
				$armor_p = "";
				$weapon_p = "";
				$alignments = "";
				$skills = "";
				$_j = 0;
				
				if($row['armor_p'] != NULL && $row['armor_p'][0]['armor_p_id'] != 0)
				{
					foreach($row['armor_p'] as $row2)
					{
						$armor_p .= $row2['ap_name'] . ",";
					}
				}
				$_j = 0;
				if($row['weapon_p'] != NULL && $row['weapon_p'][0]['weapon_p_id'] != '0')
				{
					foreach($row['weapon_p'] as $row3)
					{
						$weapon_p .= $row3['wp_name'] . ",";
					}
				}
				$_j = 0;
				if($row['alignments'] != NULL && $row['alignments'][0]['alignment_id'] != '0')
				{
					foreach($row['alignments'] as $row4)
					{
						$alignments .= $row4['a_name'] . ",";
					}
				}
				$_j = 0;
				if($row['skills'] != NULL && $row['skills'][0]['skill_id'] != '0')
				{
					foreach($row['skills'] as $row5)
					{
						$skills .= $row5['s_name'] . ",";
					}
				}
				$_j = 0;
				
				$template->assign_block_vars('list_classes', array(
					'NAME'					=> $row['name'],
					'HIT_DICE'				=> $row['hit_dice'],
					'CAST_ATTRIBUTE_NAME'	=> $row['cast_attribute_name'],
					'INFO'					=> $row['info'],
					'ARMOR_P_NAME'			=> rtrim($armor_p,","),
					'WEAPON_P_NAME'			=> rtrim($weapon_p,","),
					'ALIGNMENTS'			=> rtrim($alignments,","),
					'SKILLS'				=> rtrim($skills,","),

					'U_EDIT'				=> $row['edit_link'],
					'U_DELETE'				=> $row['delete_link'],
				));
			}
			//print_r($template);
			//$dndEdit->freeTmp();
		}
		else if ($mode == 'edit' && $inid != false)
		{
			$s_hidden_fields = array('inid' => $inid);
			$i = 0;
			
			$_class = $dndEdit->getClasses($inid)->getClasses();
			
			$template->assign_vars(array(
				'A_NAME'					=> $_class['name'],
				'A_HIT_DICE'				=> $_class['hit_dice'],
				'A_CAST_ATTRIBUTE_NAME'		=> $_class['cast_attribute_name'],
				'A_INFO'					=> $_class['info'],
			));
			
			$dndEdit->getSmall('armor_proficiencies');
			
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$is_set = 0;
				foreach($_class['armor_p'] as $row2)
				{
					if($row['id'] == $row2['armor_p_id'])
					{
						$is_set = 1;
						break;
					}
				}
				
				$template->assign_block_vars('list_armor_p', array(
					'NAME'					=> $row['name'],
					'ID'					=> $row['id'],
					'IS_SET'				=> $is_set,
				));
				$i++;
			}
			$template->assign_vars(array(
				'A_SIZE_AP'	=> $i + 1,
			));	
			$i=0;
			
			$dndEdit->getSmall('weapon_proficiencies');
			
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$is_set = 0;
				foreach($_class['weapon_p'] as $row2)
				{
					if($row['id'] == $row2['weapon_p_id'])
					{
						$is_set = 1;
						break;
					}
				}
				
				$template->assign_block_vars('list_weapon_p', array(
					'NAME'					=> $row['name'],
					'ID'					=> $row['id'],
					'IS_SET'				=> $is_set,
				));
				$i++;
			}
			$template->assign_vars(array(
				'A_SIZE_WP'	=> $i + 1,
			));	
			$i=0;
			
			$dndEdit->getSmall('alignment');
			
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$is_set = 0;
				foreach($_class['alignments'] as $row2)
				{
					if($row['id'] == $row2['alignment_id'])
					{
						$is_set = 1;
						break;
					}
				}
				
				$template->assign_block_vars('list_alignments', array(
					'NAME'					=> $row['name'],
					'ID'					=> $row['id'],
					'IS_SET'				=> $is_set,
				));
				$i++;
			}
			$template->assign_vars(array(
				'A_SIZE_AL'	=> $i + 1,
			));	
			$i=0;
			
			$dndEdit->getSkills(0);
			
			foreach($dndEdit->getTmp()->getSkills() as $row)
			{
				$is_set = 0;
				foreach($_class['skills'] as $row2)
				{
					if($row['id'] == $row2['skill_id'])
					{
						$is_set = 1;
						break;
					}
				}
				
				$template->assign_block_vars('list_skills', array(
					'NAME'					=> $row['name'],
					'ID'					=> $row['id'],
					'IS_SET'				=> $is_set,
				));
				$i++;
			}
			$template->assign_vars(array(
				'A_SIZE_SK'	=> $i + 1,
			));	
			$i=0;
			
			$dndEdit->getSmall('attribute');
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$template->assign_block_vars('list_skill_attributes', array(
					'A_ATTRIBUTE_NAME'		=> $row['name'],
					'A_ATTRIBUTE_ID'		=> $row['id'],
				));
			}
			$dndEdit->freeTmp();
			//print_r($template);
			//$dndEdit->freeTmp();
			$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'edit', 'intype' => $intype, 'inid' => $inid), true);
		}
		else if ($mode == 'insert')
		{
			$dndEdit->getSmall('armor_proficiencies');
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$is_set = 0;
				
				$template->assign_block_vars('list_armor_p', array(
					'NAME'					=> $row['name'],
					'ID'					=> $row['id'],
					'IS_SET'				=> $is_set,
				));
			}
			
			$dndEdit->getSmall('weapon_proficiencies');
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$is_set = 0;
				
				$template->assign_block_vars('list_weapon_p', array(
					'NAME'					=> $row['name'],
					'ID'					=> $row['id'],
					'IS_SET'				=> $is_set,
				));
			}
			
			$dndEdit->getSmall('alignment');
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$is_set = 0;
				
				$template->assign_block_vars('list_alignments', array(
					'NAME'					=> $row['name'],
					'ID'					=> $row['id'],
					'IS_SET'				=> $is_set,
				));
			}
			
			$dndEdit->getSkills(0);
			foreach($dndEdit->getTmp()->getSkills() as $row)
			{
				$is_set = 0;
				
				$template->assign_block_vars('list_skills', array(
					'NAME'					=> $row['name'],
					'ID'					=> $row['id'],
					'IS_SET'				=> $is_set,
				));
			}
			
			$dndEdit->getSmall('attribute');
			foreach($dndEdit->getTmp()->getSmall() as $row)
			{
				$template->assign_block_vars('list_skill_attributes', array(
					'A_ATTRIBUTE_NAME'		=> $row['name'],
					'A_ATTRIBUTE_ID'		=> $row['id'],
				));
			}
			$dndEdit->freeTmp();
			$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'submit', 'intype' => $intype), true);
		}
		else if ($mode == 'delete')
		{
			$dndEdit->getClasses($inid)->deleteClass();
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
	}
	else if($intype == 'clf')
	{
		if($mode == 'show')
		{
			
// 			print_r($dndEdit->getClassLevelFeatures(0,$in_class_id)->getClassLevelFeatures());
			
			foreach($dndEdit->getClassLevelFeatures(0,$in_class_id)->getClassLevelFeatures() as $row)
			{
				$template->assign_block_vars('list_clf', array(
					'NAME'						=> $row['name'],
					'CLASS_ID'					=> $row['class_id'],
					'LEVEL'						=> $row['class_level'],
					'BASE_ATTACK_BONUS_1'		=> $row['base_attack_bonus_1'],
					'BASE_ATTACK_BONUS_2'		=> $row['base_attack_bonus_2'],
					'BASE_ATTACK_BONUS_3'		=> $row['base_attack_bonus_3'],
					'BASE_SAVE_FORT'			=> $row['base_save_fort'],
					'BASE_SAVE_REF'				=> $row['base_save_ref'],
					'BASE_SAVE_WILL'			=> $row['base_save_will'],
					'SPECIAL_1'					=> $row['special_1']['name'],
					'SPECIAL_2'					=> $row['special_2']['name'],
					'SPECIAL_3'					=> $row['special_3']['name'],
					'SPECIAL_4'					=> $row['special_4']['name'],
					'SPECIAL_1_VALUE'			=> $row['special_1']['value'],
					'SPECIAL_2_VALUE'			=> $row['special_2']['value'],
					'SPECIAL_3_VALUE'			=> $row['special_3']['value'],
					'SPECIAL_4_VALUE'			=> $row['special_4']['value'],
					'SPELLS_PER_DAY_LVL_0'		=> $row['spells_per_day_lvl_0'],
					'SPELLS_PER_DAY_LVL_1'		=> $row['spells_per_day_lvl_1'],
					'SPELLS_PER_DAY_LVL_2'		=> $row['spells_per_day_lvl_2'],
					'SPELLS_PER_DAY_LVL_3'		=> $row['spells_per_day_lvl_3'],
					'SPELLS_PER_DAY_LVL_4'		=> $row['spells_per_day_lvl_4'],
					'SPELLS_PER_DAY_LVL_5'		=> $row['spells_per_day_lvl_5'],
					'SPELLS_PER_DAY_LVL_6'		=> $row['spells_per_day_lvl_6'],
					'SPELLS_PER_DAY_LVL_7'		=> $row['spells_per_day_lvl_7'],
					'SPELLS_PER_DAY_LVL_8'		=> $row['spells_per_day_lvl_8'],
					'SPELLS_PER_DAY_LVL_9'		=> $row['spells_per_day_lvl_9'],
					'U_EDIT'					=> $row['edit_link'],
					'U_DELETE'					=> $row['delete_link'],
					'U_CLASS_LINK'				=> $row['class_link'],
				));
			}

// 			$dndEdit->freeTmp();
// 			print_r($template);
// 			print_r($row);
		}
		else if ($mode == 'edit' && $inid != false)
		{
			$_clf = $dndEdit->getClassLevelFeatures($inid,$in_class_id)->getClassLevelFeatures();
			$template->assign_vars(array(
					'A_NAME'						=> $_clf['name'],
					'A_CLASS_ID'					=> $_clf['class_id'],
					'A_LEVEL'						=> $_clf['class_level'],
					'A_BASE_ATTACK_BONUS_1'			=> $_clf['base_attack_bonus_1'],
					'A_BASE_ATTACK_BONUS_2'			=> $_clf['base_attack_bonus_2'],
					'A_BASE_ATTACK_BONUS_3'			=> $_clf['base_attack_bonus_3'],
					'A_BASE_SAVE_FORT'				=> $_clf['base_save_fort'],
					'A_BASE_SAVE_REF'				=> $_clf['base_save_ref'],
					'A_BASE_SAVE_WILL'				=> $_clf['base_save_will'],
					'A_SPELLS_PER_DAY_LVL_0'		=> $_clf['spells_per_day_lvl_0'],
					'A_SPELLS_PER_DAY_LVL_1'		=> $_clf['spells_per_day_lvl_1'],
					'A_SPELLS_PER_DAY_LVL_2'		=> $_clf['spells_per_day_lvl_2'],
					'A_SPELLS_PER_DAY_LVL_3'		=> $_clf['spells_per_day_lvl_3'],
					'A_SPELLS_PER_DAY_LVL_4'		=> $_clf['spells_per_day_lvl_4'],
					'A_SPELLS_PER_DAY_LVL_5'		=> $_clf['spells_per_day_lvl_5'],
					'A_SPELLS_PER_DAY_LVL_6'		=> $_clf['spells_per_day_lvl_6'],
					'A_SPELLS_PER_DAY_LVL_7'		=> $_clf['spells_per_day_lvl_7'],
					'A_SPELLS_PER_DAY_LVL_8'		=> $_clf['spells_per_day_lvl_8'],
					'A_SPELLS_PER_DAY_LVL_9'		=> $_clf['spells_per_day_lvl_9'],
			));
			
			$_tmp = $dndEdit->getClasses(0)->getClasses();
			foreach($_tmp as $_class)
			{
				$template->assign_block_vars('list_clf_classes', array(
					'NAME'			=> $_class['name'],
					'ID'			=> $_class['id'],
				));
			}
			
			foreach($dndEdit->getClassLevelFeatures(0,$in_class_id)->getClassLevelFeatures() as $_level)
			{
				$template->assign_block_vars('list_clf_levels', array(
					'LEVEL'			=> $_level['class_level'],
					'U_EDIT'		=> $_level['edit_link'],
				));
			}
			$template->assign_vars(array(
					'A_CLASS_OK'						=> 1,
					));
			for($i = 0; $i < 4; $i++)
			{
				$sid = 'special_' . ($i+1);
				if($_clf[$sid] != NULL)
				{
					$_tmp = $_clf[$sid];
					$template->assign_vars(array(
						'A_CLASS_SPECIALS_' . ($i+1) . '_VALUE'						=> $_tmp['value'],
					));
// 					print_r($_tmp);
				}
				else
				{
					$_tmp = NULL;
					$template->assign_vars(array(
						'A_CLASS_SPECIALS_' . ($i+1) . '_VALUE'						=> 0,
					));
				}
				$is_set = 0;
				foreach($dndEdit->getSkills(0)->getSkills() as $row)
				{
					if($_tmp['skill_id'] != 0 && $_tmp['skill_id'] == $row['id'])
					{
						$is_set = 1;
					}
					
					$template->assign_block_vars('list_specials_' . ($i+1) . '_skills', array(
						'NAME'			=> $row['name'],
						'ID'			=> $row['id'],
						'IS_SET'		=> $is_set,
					));
					$is_set = 0;
				}
				
				foreach($dndEdit->getSmall('feat')->getSmall() as $row2)
				{
					if($_tmp['feat_id'] != 0 && $_tmp['feat_id'] == $row2['id'])
					{
						$is_set = 1;
					}
					
					$template->assign_block_vars('list_specials_' . ($i+1) . '_feats', array(
						'NAME'			=> $row2['name'],
						'ID'			=> $row2['id'],
						'IS_SET'		=> $is_set,
					));
					$is_set = 0;
				}
				
				foreach($dndEdit->getSmall('class_special')->getSmall() as $row3)
				{
					if($_tmp['class_special_id'] != 0 && $_tmp['class_special_id'] == $row3['id'])
					{
						$is_set = 1;
					}
					
					$template->assign_block_vars('list_specials_' . ($i+1) . '_class_specials', array(
						'NAME'			=> $row3['name'],
						'ID'			=> $row3['id'],
						'IS_SET'		=> $is_set,
					));
					$is_set = 0;
				}
				
			}
			$dndEdit->freeTmp();
			$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'edit', 'intype' => $intype, 'inid' => $inid, 'class_id' => $in_class_id), true);
		}
		else if ($mode == 'insert')
		{
			$_tmp = $dndEdit->getClasses(0)->getClasses();
			
			foreach($_tmp as $_class)
			{
				if($in_class_id == $_class['id'])
				{
					$is_set = 1;
					$template->assign_vars(array(
					'A_CLASS_OK'						=> 1,
					));
					foreach($dndEdit->getClassLevelFeatures(0,$in_class_id)->getClassLevelFeatures() as $_level)
					{
						$template->assign_block_vars('list_clf_levels', array(
							'LEVEL'			=> $_level['class_level'],
							'U_EDIT'		=> $_level['edit_link'],
						));
					}
				}
				$template->assign_block_vars('list_clf_classes', array(
					'NAME'			=> $_class['name'],
					'ID'			=> $_class['id'],
					'IS_SET'		=> $is_set,
				));
				$is_set = 0;
			}
			
			for($i = 0; $i < 4; $i++)
			{
				$sid = 'special_id_' . ($i+1);
				$is_set = 0;
				foreach($dndEdit->getSkills(0)->getSkills() as $row)
				{
					$template->assign_block_vars('list_specials_' . ($i+1) . '_skills', array(
						'NAME'			=> $row['name'],
						'ID'			=> $row['id'],
						'IS_SET'		=> $is_set,
					));
				}
				
				foreach($dndEdit->getSmall('feat')->getSmall() as $row2)
				{
					$template->assign_block_vars('list_specials_' . ($i+1) . '_feats', array(
						'NAME'			=> $row2['name'],
						'ID'			=> $row2['id'],
						'IS_SET'		=> $is_set,
					));
				}
				
				foreach($dndEdit->getSmall('class_special')->getSmall() as $row3)
				{
					$template->assign_block_vars('list_specials_' . ($i+1) . '_class_specials', array(
						'NAME'			=> $row3['name'],
						'ID'			=> $row3['id'],
						'IS_SET'		=> $is_set,
					));
				}
				
				
			}
			$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'submit', 'intype' => $intype, 'inid' => $inid, 'class_id' => $in_class_id), true);
		}
		else if ($mode == 'delete')
		{
			$_clf = $dndEdit->getClassLevelFeatures($inid,$in_class_id);
			$_clf->deleteClassLevelFeature();
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
	}
	else
	{
		//TODO Class Object
		//echo "not implemented yet!";
		trigger_error("Wrong mode or not implemented yet!");
	}
	
	/*
	switch($intype)
	{
		case 'alignment':
			if($mode == 'show')
			{
				$dndEdit->getAlignments();
				foreach($dndEdit->tmp as $row)
				{
					//var_dump($row);
					$template->assign_block_vars('list_alignments', array(
						'NAME'		=> $row['name'],
						'SHORT'		=> $row['short'],
						'INFO'		=> $row['info'],
						'U_EDIT'	=> $row['edit_link'],
						'U_DELETE'	=> $row['delete_link'],
					));
				}
				$dndEdit->freeTmp();
			}
			else if ($mode == 'edit' && $inid != false)
			{
				$s_hidden_fields = array('inid' => $inid);
				$dndEdit->getAlignmentByID($inid);
				
				$template->assign_vars(array(
					'A_NAME'	=> $dndEdit->tmp->small['name'],
					'A_SHORT'	=> $dndEdit->tmp->small['short'],
					'A_INFO'	=> $dndEdit->tmp->small['info'],
				));
				$dndEdit->freeTmp();
				$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'edit', 'intype' => $intype, 'inid' => $inid), true);
			}
			else if ($mode == 'insert')
			{
				$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'submit', 'intype' => $intype), true);
			}
			else if ($mode == 'delete')
			{
				$dndEdit->getAlignmentByID($inid);
				//var_dump($dndEdit->tmp);
				$dndEdit->tmp->deleteSmall();
				$dndEdit->freeTmp();
				redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
			}
		break;
		
		case 'armor_proficiencies':
				//todo
			if($mode == 'show')
			{
				$dndEdit->getArmorProficiencies();
				foreach($dndEdit->tmp as $row)
				{
					//var_dump($row);
					$template->assign_block_vars('list_armor_proficiencies', array(
						'NAME'		=> $row['name'],
						'INFO'		=> $row['info'],
						'U_EDIT'	=> $row['edit_link'],
						'U_DELETE'	=> $row['delete_link'],
					));
				}
				$dndEdit->freeTmp();
			}
			else if ($mode == 'edit' && $inid != false)
			{
				$s_hidden_fields = array('inid' => $inid);
				$dndEdit->getArmorProficienciesByID($inid);
				
				$template->assign_vars(array(
					'A_NAME'	=> $dndEdit->tmp->small['name'],
					'A_INFO'	=> $dndEdit->tmp->small['info'],
				));
				$dndEdit->freeTmp();
				$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'edit', 'intype' => $intype, 'inid' => $inid), true);
			}
			else if ($mode == 'insert')
			{
				$s_action = append_sid("{$phpbb_root_path}dnd_edit.$phpEx", array('mode' => 'submit', 'intype' => $intype), true);
			}
			else if ($mode == 'delete')
			{
				$dndEdit->getArmorProficienciesByID($inid);
				//var_dump($dndEdit->tmp);
				$dndEdit->tmp->deleteSmall();
				$dndEdit->freeTmp();
				redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
			}
		break;
		
		default:
			
		break;
	}
	*/
	
	$template->assign_vars(array(
		'S_INTYPE'				=> $intype,
		'S_POST_ACTION'			=> $s_action,
		'S_MODE'				=> $mode,
		'S_CLASS_ID'			=> $in_class_id,
//		'S_HIDDEN_FIELDS'		=> $s_hidden_fields,
		
		'U_LINK_ALIGNMENT'				=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'alignment')),
		'U_LINK_ARMOR_PROFICIENCIES'	=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'armor_proficiencies')),
		'U_LINK_CLASS'					=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'classe')),
		'U_LINK_CLASS_LF'				=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'clf')),
		'U_LINK_CLASS_SP'				=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'class_special')),
		'U_LINK_DEITY'					=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'deity')),
		'U_LINK_FEAT'					=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'feat')),
		'U_LINK_GROUP'					=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'group')),
		'U_LINK_RACE'					=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'race')),
		'U_LINK_SIZE'					=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'size')),
		'U_LINK_SKILL'					=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'skill')),
		'U_LINK_SPELL'					=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'spell')),
		'U_LINK_WEAPON_P'				=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => 'weapon_proficiencies')),
		'U_LINK_NEW'					=> append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('intype' => $intype, 'mode' => 'insert')),
		
		
		'EOF_PLACEHOLDER'			=> '')
	);
}

if($submit)
{
	if($intype != 'classe' && $intype != 'spell' && $intype != 'skill' && $intype != 'clf')
	{
		if($mode == 'edit')
				{
					$dndEdit->getSmallByID($inid,$intype);
					$dndEdit->getTmp()->setSmall($inid, $in_name, $in_info, $intype, $in_extra, 'UPDATE');
					redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
					
				}
		else if($mode == 'submit')
				{
					$dndEdit->getSmallByID(0, $intype);
					$dndEdit->getTmp()->setSmall(0, $in_name, $in_info, $intype, $in_extra, 'INSERT');
					redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
				}
		$dndEdit->freeTmp();
	}
	else if($intype == 'skill')
	{
		if($mode == 'edit')
		{
			//if($in_skill['armor_check_penalty'] == NULL) $in_skill['armor_check_penalty'] = 0;
			$dndEdit->getSkillByID($inid);
			$dndEdit->getTmp()->setSkill(
									$inid, 
									$in_skill['name'],
									$in_skill['info'],
									$in_skill['attribute_id'],
									$in_skill['use_untrained'],
									$in_skill['armor_check_penalty'],
									'UPDATE'
									);
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
		else if($mode == 'submit')
		{
			//if($in_skill['armor_check_penalty'] == NULL) $in_skill['armor_check_penalty'] = 0;
			$dndEdit->getSkillByID(-1);
			
			$dndEdit->getTmp()->setSkill(
									$inid, 
									$in_skill['name'],
									$in_skill['info'],
									$in_skill['attribute_id'],
									$in_skill['use_untrained'],
									$in_skill['armor_check_penalty'],
									'INSERT'
									);
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
		$dndEdit->freeTmp();
	}
	else if($intype == 'spell')
	{
		if($mode == 'edit')
		{
			$dndEdit->getSpellByID($inid,$intype);
			$dndEdit->getTmp()->setSpell(
									$inid, 
									$in_spell['name'],
									$in_spell['school'],
									$in_spell['level'],
									$in_spell['components'],
									$in_spell['casting_time'],
									$in_spell['range'],
									$in_spell['effect'],
									$in_spell['duration'],
									$in_spell['saving_throw'],
									$in_spell['spell_resistance'],
									$in_spell['info'],
									'UPDATE'
									);
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
		else if($mode == 'submit')
		{
			$dndEdit->getSpellByID(-1,$intype);
			$dndEdit->getTmp()->setSpell(
									0, 
									$in_spell['name'],
									$in_spell['school'],
									$in_spell['level'],
									$in_spell['components'],
									$in_spell['casting_time'],
									$in_spell['range'],
									$in_spell['effect'],
									$in_spell['duration'],
									$in_spell['saving_throw'],
									$in_spell['spell_resistance'],
									$in_spell['info'],
									'INSERT'
									);
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
		$dndEdit->freeTmp();
	}
	else if($intype == 'classe')
	{
		$data_ap = array();
		$data_wp = array();
		$data_al = array();
		$data_sk = array();
		$i = 0;
		
		//$dndEdit->getSmall($inid,"armor_proficiencies");
		
		
		if($in_class[list_armor_p] != NULL)
		{
			foreach($in_class[list_armor_p] as $_data)
			{
				$data_ap[$i]['class_id']	= $inid;
				$data_ap[$i]['armor_p_id']	= $_data;
				$i++;
			}
		}
		$i = 0;
		if($in_class[list_weapon_p] != NULL)
		{
			foreach($in_class[list_weapon_p] as $_data)
			{
				$data_wp[$i]['class_id']	= $inid;
				$data_wp[$i]['weapon_p_id']	= $_data;
				$i++;
			}
		}
		$i = 0;
		if($in_class[list_alignments] != NULL)
		{
			foreach($in_class[list_alignments] as $_data)
			{
				$data_al[$i]['class_id']	= $inid;
				$data_al[$i]['alignment_id']	= $_data;
				$i++;
			}
		}
		$i = 0;
		if($in_class[list_skills] != NULL)
		{
			foreach($in_class[list_skills] as $_data)
			{
				$data_sk[$i]['class_id']	= $inid;
				$data_sk[$i]['skill_id']	= $_data;
				$i++;
			}
		}
		
		if($mode == 'edit')
		{
			$dndEdit->getClasses($inid)->setClass(
									$inid, 
									$in_class['name'],
									$in_class['hit_dice'],
									$in_class['cast_attribute_id'],
									$data_ap,
									$data_wp,
									$data_al,
									$data_sk,
									$in_class['info'],
									'UPDATE'
									);
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
			
		}
		else if($mode == 'submit')
		{
			$dndEdit->getClasses($inid)->setClass(
									$inid, 
									$in_class['name'],
									$in_class['hit_dice'],
									$in_class['cast_attribute_id'],
									$data_ap,
									$data_wp,
									$data_al,
									$data_sk,
									$in_class['info'],
									'INSERT'
									);
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
		
		$dndEdit->freeTmp();
	}
	else if($intype == 'clf')
	{
// 		print_r($in_clf);
		$data_specials = array();
		$data_spd = array();
		$i = 0;
		
		//$dndEdit->getSmall($inid,"armor_proficiencies");
		
		for($j = 1; $j < 5; $j++)
		{
			if($in_clf['special_'. $j .'_feat_id'] != 0 || $in_clf['special_'. $j .'_skill_id'] != 0 || $in_clf['special_'. $j .'_special_id'] != 0)
			{
				if($in_clf['special_'. $j .'_feat_id'] != 0)
				{
					$data_specials[$j] = array(
						'class_id'			=> $in_class_id,
						'class_level'		=> $in_clf['level'],
						'skill_id'			=> 0,
						'feat_id'			=> $in_clf['special_'. $j .'_feat_id'],
						'class_special_id'	=> 0,
						'value'				=> $in_clf['special_'. $j .'_value'],
					);
				}
				if($in_clf['special_'. $j .'_skill_id'] != 0)
				{
					$data_specials[$j] = array(
						'class_id'			=> $in_class_id,
						'class_level'		=> $in_clf['level'],
						'skill_id'			=> $in_clf['special_'. $j .'_skill_id'],
						'feat_id'			=> 0,
						'class_special_id'	=> 0,
						'value'				=> $in_clf['special_'. $j .'_value'],
					);
				}
				if($in_clf['special_'. $j .'_special_id'] != 0)
				{
					$data_specials[$j] = array(
						'class_id'			=> $in_class_id,
						'class_level'		=> $in_clf['level'],
						'skill_id'			=> 0,
						'feat_id'			=> 0,
						'class_special_id'	=> $in_clf['special_'. $j .'_special_id'],
						'value'				=> $in_clf['special_'. $j .'_value'],
					);
				}
				
				
			}
		}
		
		for($i = 0; $i < 10; $i++)
		{
			$data_spd[$i] = $in_clf['spells_per_day_lvl_'. $i];
		}
// 		echo "\n\n";
		print_r($data_spd);
		print_r($in_clf);
		
		
		if($mode == 'edit')
		{
			$dndEdit->getClassLevelFeatures($inid,$in_class_id)->setClassLevelFeatures(
									$inid,
									$in_class_id,
									$in_clf['level'],
									$in_clf['base_attack_bonus_1'],
									$in_clf['base_attack_bonus_2'],
									$in_clf['base_attack_bonus_3'],
									$in_clf['base_save_fort'],
									$in_clf['base_save_ref'],
									$in_clf['base_save_will'],
									$data_specials,
									$data_spd,
									'UPDATE'
									);
// 			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
			
		}
		else if($mode == 'submit')
		{
			echo $in_class_id;
			$dndEdit->getClassLevelFeatures($inid,$in_class_id)->setClassLevelFeatures(
									$inid,
									$in_class_id,
									$in_clf['level'],
									$in_clf['base_attack_bonus_1'],
									$in_clf['base_attack_bonus_2'],
									$in_clf['base_attack_bonus_3'],
									$in_clf['base_save_fort'],
									$in_clf['base_save_ref'],
									$in_clf['base_save_will'],
									$data_specials,
									$data_spd,
									'INSERT'
									);
			redirect(append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => 'show', 'intype' => $intype)));
		}
	}
	else
	{
		trigger_error("Not implemented yet!");
	}
}

function getEditLink($intype,$mode,$inid)
{
	return append_sid($phpbb_root_path.'dnd_edit.'.$phpEx, array('mode' => $mode, 'intype' => $intype, 'inid' => $inid));
}

function isIntype($intype)
{
	$types = array(		'alignment',
						'armor_proficiencies',
						'attribute',
						'class',
						'clf',
						'class_special',
						'deity',
						'feat',
						'group',
						'race',
						'size',
						'skill',
						'weapon_proficiencies',
						'spell');
						
	foreach($types as $type)
	{
		if($intype == $type)
		{
			return true;
		}
	}
	
	return false;
}
// Footer
page_footer();

?>